import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '../components/Login')
const Home = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '../components/Home')
const Welcome = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '../components/Welcome')

// 用户
const Users = () =>
    import ( /* webpackChunkName: "users_rights_roles" */ '../components/user/Users')
const Rights = () =>
    import ( /* webpackChunkName: "users_rights_roles" */ '../components/rights/Rights')
const Roles = () =>
    import ( /* webpackChunkName: "users_rights_roles" */ '../components/rights/Roles')

// 商品
const Cate = () =>
    import ( /* webpackChunkName: "cate_goods_params_addgoods" */ '../components/goods/Cate')
const Goods = () =>
    import ( /* webpackChunkName: "cate_goods_params_addgoods" */ '../components/goods/GoodsList')
const Params = () =>
    import ( /* webpackChunkName: "cate_goods_params_addgoods" */ '../components/goods/GoodsParams')
const AddGoods = () =>
    import ( /* webpackChunkName: "cate_goods_params_addgoods" */ '../components/goods/AddGoods')

//订单
const Order = () =>
    import ( /* webpackChunkName: "order" */ '../components/order/Order')

//报表
const Report = () =>
    import ( /* webpackChunkName: "report" */ '../components/report/Report')


Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/users', component: Users },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Roles },
            { path: '/categories', component: Cate },
            { path: '/goods', component: Goods },
            { path: '/params', component: Params },
            { path: '/goods/add', component: AddGoods },
            { path: '/orders', component: Order },
            { path: '/reports', component: Report },
        ]
    },

]

const router = new VueRouter({
        routes
    })
    // 挂载路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next();
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    next()
})

export default router